<template>
  <div>
    <div class="title">
      <i class="el-icon-first-aid-kit">美奥口腔医院管理账号注册</i>
    </div>
    <video
        style="position: absolute; z-index: -1; width: 1900px"
        class="mp41"
        src="../assets/img/背景视频.mp4"
        autoplay="autoplay"
        loop="loop"
        muted="muted"
    ></video>
    <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="
        color: #fff;
        position: absolute;
        z-index: 1;
        width: 500px;
        margin-left: 650px;
        margin-top: 200px;
        color: white;
      "
    >
      <el-form-item label="用户名" prop="username" style="color: #fff">
        <el-input v-model.number="ruleForm.username"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="check_pwd">
        <el-input
            type="password"
            v-model="ruleForm.check_pwd"
            autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="注册类型" prop="user_type">
        <el-radio v-model="ruleForm.user_type" label="1">管理员</el-radio>
        <el-radio v-model="ruleForm.user_type" label="2">医生</el-radio>
        <el-radio v-model="ruleForm.user_type" label="3">药管师</el-radio>
      </el-form-item>

      <div v-if="ruleForm.user_type!=='1'">
        <el-form-item label="所属科室" prop="department">
          <el-select v-model="ruleForm.doctor.department" placeholder="请选择所在科室">
            <el-option
                v-for="item in department_data_type"
                :key="item.department_id"
                :label="item.department_label"
                :value="item.department_id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="真实姓名" prop="name" style="color: #fff">
          <el-input v-model="ruleForm.doctor.name" style="color: #fff"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile" style="color: #fff">
          <el-input v-model="ruleForm.doctor.mobile" style="color: #fff"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="ruleForm.doctor.gender" placeholder="请选择性别">
            <el-option
                v-for="item in gender"
                :key="item.gender_id"
                :label="item.gender_label"
                :value="item.gender_id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="出生年月" prop="data_birth">
          <el-date-picker
              v-model="ruleForm.doctor.data_birth"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </div>


      <el-form-item>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
        >提交
        </el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script scoped>
export default {
  'name':'register',
  mounted() {
    window.Vue = this;
  },
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        this.is_check = false
        return callback(new Error("用户名不能为空"));
      }
      this.is_check = true
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        this.is_check = false
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.check_pwd !== "") {
          this.$refs.ruleForm.validateField("check_pwd");
        }
        this.is_check = true
        callback();
      }
    };
    var validate_mobile = (rule, value, callback) => {
      let check_mobile = this.ruleForm.doctor.mobile
      if (!check_mobile) {
        return callback(new Error("电话号码不能为空"))
      }
      if (!check_mobile.match(/^1[3-9][0-9]{9}$/)) {
        this.is_check = false
        callback(new Error("电话号码不规范"))
      } else {
        this.$axios.get(this.$settings.BASE_URL + 'user/mobile/get_mobile/?mobile=' + check_mobile).then(response => {
          if (response.data.code !== 100) {
            this.is_check = false
            callback(new Error("电话号码已注册"))
          } else {
            this.is_check = true
            callback()
          }
        })
      }
    };
    var validate_name = (rule, value, callback) => {
      if (!this.ruleForm.doctor.name) {
        this.is_check = false
        return callback(new Error("姓名不能为空"))
      } else {
        this.is_check = true
        callback()
      }
    };
    var validate_user_type = (rule, value, callback) => {
      if (!this.ruleForm.user_type) {
        this.is_check = false
        return callback(new Error("请选择用户类型"))
      } else {
        this.is_check = true
        callback()
      }
    };
    var validate_department = (rule, value, callback) => {
      if (!this.ruleForm.doctor.department) {
        this.is_check = false
        return callback(new Error("请选择所属科室"))
      } else {
        this.is_check = true
        callback()
      }
    };
    var validate_gender = (rule, value, callback) => {
      if (!this.ruleForm.doctor.gender) {
        this.is_check = false
        return callback(new Error("请选择性别"))
      } else {
        this.is_check = true
        callback()
      }
    };
    var validate_data_birth = (rule, value, callback) => {
      if (!this.ruleForm.doctor.data_birth) {
        this.is_check = false
        return callback(new Error("请选择出生日期"))
      } else {
        this.is_check = true
        callback()
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        this.is_check = false
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        console.log(value)
        console.log(rule)
        console.log(callback)
        this.is_check = false
        callback(new Error("两次输入密码不一致!"));
      } else {
        this.is_check = true
        callback();
      }
    };

    return {
      is_check: false,
      gender: [{
        gender_id: '0',
        gender_label: '男',
      }, {
        gender_id: '1',
        gender_label: '女',
      },
        {
          gender_id: '2',
          gender_label: '其他',
        }],
      gender_id: '0',
      department_data_type: [{}],
      department_id: '1',

      ruleForm: {
        username: "",
        password: "",
        check_pwd: "",
        user_type: "",
        doctor: {
          department: '',
          name: "",
          gender: '',
          data_birth: "",
          mobile: "",
        },

      },
      rules: {
        password: [{validator: validatePass, trigger: "blur"}],
        check_pwd: [{validator: validatePass2, trigger: "blur"}],
        username: [{validator: checkAge, trigger: "blur"}],
        mobile: [{validator: validate_mobile, trigger: ["blur", "change"]}],
        name: [{validator: validate_name, trigger: ["blur", "change"]}],
        department: [{validator: validate_department, trigger: ["blur", "change"]}],
        user_type: [{validator: validate_user_type, trigger: ["blur", "change"]}],
        gender: [{validator: validate_gender, trigger: ["blur", "change"]}],
        data_birth: [{validator: validate_data_birth, trigger: ["blur",]}],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log('1')

      this.$refs[formName].validate((valid) => {
        console.log('错误')
      });
      console.log(this.ruleForm.user_type)
      if (this.ruleForm.user_type == '1') {
        delete this.ruleForm.doctor
        console.log(2)
      }
      if (this.is_check) {
        this.$axios({


          url: this.$settings.BASE_URL + 'user/doc_register/register/',
          method: "POST",
          data: this.ruleForm
        }).then((Response) => {
          let user = Response.data;
          if (user.code === 100) {
            // this.$setSessionStorage("user", user);
            alert(user.msg);
            this.$router.push({
              path: "/",
            });
          } else {
            alert("账号注册失败", user.msg);
          }
        })
            .catch((error) => {
              console.error(error);
            });
      }

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    this.$axios.get(this.$settings.BASE_URL + 'user/dep_register/reg_department/',).then(response => {
      this.department_data_type = ''
      this.department_data_type = response.data.departments_data_type;

      // console.log('科室',response.data.departments_data_type)
      // console.log('科室',response.data)
      // this.user_type_data=response.data.result.user_type_data;

    }).catch(error => {
      console.log(error.response);
    })
  }
};
</script>

<style scoped>
.title {
  position: absolute;
  z-index: 0;
  margin-left: 630px;
  margin-top: 50px;
  color: #d78484;
  font-size: 50px;
}

label,
.el-form-item__label {
  color: #f59999;
}
</style>